package com.elasticsearch.wenzi.elastic.response;


import com.elasticsearch.wenzi.elastic.enums.ResponseCodeEnum;
import com.elasticsearch.wenzi.elastic.enums.ResultEnum;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

/**
 * 通用返回值
 * Created by zhaozuowen
 */
@ApiModel(description = "基础返回")
public class ResultResponse<T> implements Serializable {

	private static final long serialVersionUID = 1L;
	
	@ApiModelProperty(value="请求处理结果")
	private boolean success = true;
    /**
     * 返回码
     */
    @ApiModelProperty(value="返回码")
    private int retCode = ResultEnum.SUCCESS.getCode();
    /**
     * 返回信息
     */
    @ApiModelProperty(value="返回信息")
    private String retMsg = ResultEnum.SUCCESS.getMsg();
    /**
     * 业务数据
     */
    @ApiModelProperty(value="返回数据")
    private T data;

    @ApiModelProperty(value = "验签串，这里只做data的数据部分")
    private String bodyEncry;

    //自定义属性
    @ApiModelProperty(value="自定义属性")
    private Map<String,String> extMap;

    private void checkAndInitExtMap() {
        if(extMap==null) {
            synchronized(this) {
                if(extMap==null) {
                    extMap = new HashMap<String,String>();
                }
            }
        }
    }

    public ResultResponse<T> setExt(String key, String value){
        checkAndInitExtMap();
        extMap.put(key, value);
        return this;
    }

    public ResultResponse<T> removeExt(String key){
        checkAndInitExtMap();
        extMap.remove(key);
        return this;
    }

    public Map<String, String> getExtMap() {
        return extMap;
    }

    public void setExtMap(Map<String, String> extMap) {
        this.extMap = extMap;
    }
    
    public static <T> ResultResponse<T> success() {
		ResultResponse<T> result = new ResultResponse<T>();
		return result;
	}
    
    public static <T> ResultResponse<T> success(T data) {
		ResultResponse<T> result = new ResultResponse<T>();
		result.setData(data);
		return result;
	}
    
    public static <T> ResultResponse<T> success(String retMsg, T data) {
		ResultResponse<T> result = new ResultResponse<T>();
		result.setRetMsg(retMsg);
		result.setData(data);
		return result;
	}
    
    public static <T> ResultResponse<T> fail(String retMsg) {
		ResultResponse<T> result = new ResultResponse<T>();
		result.setRetCode(ResultEnum.ERROR.getCode());
		result.setRetMsg(retMsg);
		return result;
	}
    
    public static <T> ResultResponse<T> fail(int retCode, String retMsg) {
		ResultResponse<T> result = new ResultResponse<T>();
		result.setRetCode(retCode);
		result.setRetMsg(retMsg);
		return result;
	}
    
    public static <T> ResultResponse<T> fail(ResponseCodeEnum resultEnum) {
		ResultResponse<T> result = new ResultResponse<T>();
		result.setResultEnum(resultEnum);
		return result;
	}
    
    public int getRetCode() {
        return retCode;
    }

    public void setRetCode(int retCode) {
        if (retCode < 0) {
            this.success = false;
        } else {
            this.success = true;
        }
        this.retCode = retCode;
    }

    public String getRetMsg() {
        return retMsg;
    }

    public void setRetMsg(String retMsg) {
        this.retMsg = retMsg;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public boolean isSuccess() {
        return success;
    }

    public void setSuccess(boolean success) {
        this.success = success;
    }
    

	public String getBodyEncry() {
		return bodyEncry;
	}

	public void setBodyEncry(String bodyEncry) {
		this.bodyEncry = bodyEncry;
	}

	public void setResultEnum(ResponseCodeEnum resultEnum) {
    	setRetMsg(resultEnum.getMsg());
    	setRetCode(resultEnum.getCode());
    }
}
